package com.example.fw;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class HelperBase {
	protected final ApplicationManager manager;
	protected Logger log = Logger.getLogger("FW");
	
	public HelperBase(ApplicationManager manager) {
		this.manager = manager;	
	}
	
	protected WebDriver getDriver() {
		return manager.getWebDriverHelper().getDriver();
	}

	protected WebElement findElement(By locator) {
		return getDriver().findElement(locator);
	}

	protected List<WebElement> findElements(By locator) {
		getDriver().manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
		List<WebElement> elements = getDriver().findElements(locator);
		getDriver().manage().timeouts().implicitlyWait(Integer.parseInt(manager.getProperty("implicitlyWait", "10")), TimeUnit.SECONDS);
		return elements;
	}

	protected List<WebElement> findElements(WebElement upperElement, By locator) {
		getDriver().manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
		List<WebElement> elements = upperElement.findElements(locator);
		getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		return elements;
	}
	
	protected boolean isElementPresent(By locator) {		
		return findElements(locator).size() > 0;
	}
	
	protected void click(By locator) {
		findElement(locator).click();
	}
	
	protected void type(By locator, String value) {
		findElement(locator).clear();
		findElement(locator).sendKeys(value);		
	}
	
	protected void selectValueFromTheList(By locator, String value) {
		new Select(findElement(locator)).selectByVisibleText(value);
	}
	
	protected void openUrl(String string) {
		getDriver().get(string);
	}
	
	protected void checkElementByID(String id) {
		click(By.cssSelector("input[value=\"" + id + "\"]"));
	}
}
